package com.txzt.gaea.upgrade.project.controller;


import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.base.service.DictInfoService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.ProgressStatus;
import com.txzt.gaea.upgrade.project.domain.model.ProjectDelay;
import com.txzt.gaea.upgrade.project.domain.model.ProjectMain;
import com.txzt.gaea.upgrade.project.domain.model.ProjectStop;
import com.txzt.gaea.upgrade.project.domain.vo.ProjectMainVo;
import com.txzt.gaea.upgrade.project.service.IProjectMainService;
import com.txzt.gaea.upgrade.project.service.IProjectProgressService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


@RestController
@RequestMapping("/project/main")
public class ProjectMainController extends BaseController {


    @Autowired
    private IProjectMainService projectMainService;
    @Autowired
    private DictInfoService dictInfoService;
    @Autowired
    private IProjectProgressService projectProgressService;

    /**
     * 分页查询
     *
     * @param pageNumber
     * @param pageSize
     * @param projectMain
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectMain projectMain) {
        PageQuery<ProjectMain> pageList = projectMainService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectMain));
        return RtnInfo.success(translate(pageList));
    }

    /**
     * 查询待审核数量(开题报告管理，结题管理，延期完成申请，终止研究申请)
     * @param projectMain
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/listCheckCount")
    public RtnInfo<?> listCheckCount(ProjectMain projectMain) {
        Long count = projectMainService.listCheckCount(projectMain);
        return RtnInfo.success(count);
    }

    /**
     * 分页查询
     *
     * @param pageNumber
     * @param pageSize
     * @param projectMain
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/query")
    public RtnInfo<?> query(@RequestParam(defaultValue = "1") Integer pageNumber,
                            @RequestParam(defaultValue = "10") Integer pageSize, ProjectMain projectMain) {
        PageQuery<ProjectMain> pageList = projectMainService.pageQuery("projectMain.queryProjectMainByCondition",new PageQuery<>(pageNumber, pageSize, projectMain));
        pageList.getList().forEach(item -> {
            item.setProgressList(projectProgressService.selectProjectProgressByProjectId(item.getId()));
        });
        return RtnInfo.success(translate(pageList));
    }

    private PageQuery<ProjectMain> translate(PageQuery<ProjectMain> pageList) {
        pageList.getList().forEach(item -> {
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
            //翻译承担部门
            item.setCreateByName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译承担部门
            item.setDeptName(TranslateUtil.getDepartmentName(item.getDeptId()));
        });
        return pageList;
    }

    /**
     * 仅编辑
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/editOnly")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> editOnly(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.updateById(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 开题拟制 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/openDraftEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> openDraftEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.openDraftEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 开题评审 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/openReviewEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> openReviewEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.openReviewEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 开题评审 - 汇总评审
     *
     * @param mainList
     * @return
     */
    @PutMapping(value = "/openReviewCollect")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> openReviewCollect(@RequestBody List<ProjectMain> mainList) {
        int count = projectMainService.openReviewCollect(mainList);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }



    /**
     * 开题提报 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/openSubmitEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> openSubmitEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.openSubmitEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 开题提报 - 汇总提报
     *
     * @param mainList
     * @return
     */
    @PutMapping(value = "/openSubmitCollect")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> openSubmitCollect(@RequestBody List<ProjectMain> mainList) {
        int count = projectMainService.openSubmitCollect(mainList);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 结题申请 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/finishApplyEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finishApplyEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.finishApplyEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 结题评审 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/finishReviewEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finishReviewEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.finishReviewEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 结题评审 - 汇总评审
     *
     * @param mainList
     * @return
     */
    @PutMapping(value = "/finishReviewCollect")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finishReviewCollect(@RequestBody List<ProjectMain> mainList) {
        int count = projectMainService.finishReviewCollect(mainList);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 结题呈报 - 编辑/提交
     *
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/finishSubmitEdit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finishSubmitEdit(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.finishSubmitEdit(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 结题呈报 - 汇总呈报
     *
     * @param mainList
     * @return
     */
    @PutMapping(value = "/finishSubmitCollect")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finishSubmitCollect(@RequestBody List<ProjectMain> mainList) {
        int count = projectMainService.finishSubmitCollect(mainList);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }



    /**
     * 项目延期 - 提交
     *
     * @param projectDelay
     * @return
     */
    @PutMapping(value = "/delay")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> projectDelay(@RequestBody ProjectDelay projectDelay) {
        int count = projectMainService.projectDelay(projectDelay);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 项目终止 - 提交
     *
     * @param projectStop
     * @return
     */
    @PutMapping(value = "/stop")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> projectStop(@RequestBody ProjectStop projectStop) {
        int count = projectMainService.projectStop(projectStop);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 项目延期 - 项目id查询，获取项目所有延期申请记录
     *
     * @param projectId
     * @return
     */
    @GetMapping(value = "/getDelay")
    public RtnInfo<?> getDelay(@RequestParam Long projectId) {
        return RtnInfo.success(projectMainService.getDelayByProjectId(projectId));
    }

    /**
     * 项目终止 - 项目id查询，获取项目所有终止申请记录
     *
     * @param projectId
     * @return
     */
    @GetMapping(value = "/getStop")
    public RtnInfo<?> getStop(@RequestParam Long projectId) {
        return RtnInfo.success(projectMainService.getStopByProjectId(projectId));
    }


    /**
     * 项目延期 - id查询，获取项目当前延期申请记录
     *
     * @param delayId
     * @return
     */
    @GetMapping(value = "/queryByDelayId")
    public RtnInfo<?> queryByDelayId(@RequestParam Long delayId) {
        return RtnInfo.success(projectMainService.queryByDelayId(delayId));
    }

    /**
     * 项目终止 - id查询，获取项目当前终止申请记录
     *
     * @param stopId
     * @return
     */
    @GetMapping(value = "/queryByStopId")
    public RtnInfo<?> queryByStopId(@RequestParam Long stopId) {
        return RtnInfo.success(projectMainService.queryByStopId(stopId));
    }



    /**
     * 撤回
     * @param projectMain
     * @return
     */
    @PutMapping(value = "/revocation")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> revocation(@RequestBody ProjectMain projectMain) {
        int count = projectMainService.revocationProjectMain(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.error(99,"项目已开始审核，不可撤回！");
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ProjectMain projectMain = new ProjectMain();
        projectMain.setId(id);
        projectMain.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = projectMainService.updateById(projectMain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }



    /**
     * 根据类型，年份查询项目经费信息
     *
     * @param projectMainVo
     * @return
     */
    @GetMapping(value = "/projectFund")
    public RtnInfo<?> projectFund(ProjectMainVo projectMainVo) {
        List<ProjectMainVo> list = projectMainService.getProjectFund(projectMainVo);
        list.forEach(item ->{
            item.setLedgerAccountName(dictInfoService.dictTranslate("ledger_account",item.getLedgerAccount()));
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(list);
    }

    /**
     * 根据类型，年份查询项目经费信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/progressFinish")
    public RtnInfo<?> progressFinish(@RequestParam Long id) {
        ProjectMain projectMain = new ProjectMain();
        projectMain.setId(id);
        projectMain.setProgressStatus(ProgressStatus.DONE.getCode());
        return RtnInfo.success(projectMainService.updateById(projectMain));
    }



    @PostMapping(value = "/import")
    public RtnInfo<?> importWord(MultipartFile file) throws Exception {
        XWPFDocument xwpf = new XWPFDocument(file.getInputStream());
        xwpf.getParagraphs().forEach(paragraph ->{
            System.out.println(" -----  |paragraph| -----");
            System.out.println(paragraph.getText());
        });
        AtomicReference<Integer> num = new AtomicReference<>(0);
        List<XWPFTable> tableList = xwpf.getTables();
        tableList.forEach(p -> {
            System.out.println(" ------------ 《table" + num + " 》------------");
            p.getRows().forEach(row -> {
                        System.out.println(" ------------ 【row】 ------------");
                        row.getTableCells().forEach(cell -> {
                                    System.out.println(" -----  （cell） -----");
                                    System.out.println(cell.getText());
                                }
                        );
                    }
            );
            num.getAndSet(num.get() + 1);
        });
        return RtnInfo.SUCCESS;
    }

    /**
     * 项目延期申请信息导出
     * @param id
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportDelay")
    public void exportWord(Long id, HttpServletResponse response) throws Exception {
        projectMainService.exportWordProjectDelay(id,response);
    }

    /**
     * 项目终止申请信息导出
     * @param id
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportStop")
    public void exportStop(Long id, HttpServletResponse response) throws Exception {
        projectMainService.exportWordProjectStop(id,response);
    }

    /**
     * 项目结题申请信息导出
     * @param id
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/exportFinish")
    public void exportFinish(Long id, HttpServletResponse response) throws Exception {
        projectMainService.exportWordProjectFinish(id,response);
    }

}
